package com.zablert.lazyutil.utils

import com.intellij.openapi.vfs.LocalFileSystem
import com.zablert.lazyutil.drawable.ConfirmationDialog
import java.io.*
import javax.annotation.Nonnull

object FileUtil {
    /**
     * 替换模板中字符
     *
     * @param  content 文件
     * @param tag     占位符
     * @param value   替换值
     */
    private fun dealTemplateContent(@Nonnull content: String, @Nonnull tag: String, @Nonnull value: String): String {
        var content = content
        if (content.contains(tag)) {
            content = content.replace(tag, value)
        }
        return content
    }

    @Nonnull
    private fun readFile(@Nonnull fileNamePath: String): String {
        val `in` = this.javaClass.getResourceAsStream(fileNamePath)
        return if (`in` != null) {
            String(readStream(`in`))
        } else ""
    }

    /**
     * 读取数据
     */
    @Nonnull
    private fun readStream(@Nonnull inputStream: InputStream): ByteArray {
        val outputStream = ByteArrayOutputStream()
        try {
            inputStream.use {
                outputStream.use {
                    val buffer = ByteArray(1024)
                    var len: Int
                    while (inputStream.read(buffer).also { len = it } != -1) {
                        outputStream.write(buffer, 0, len)
                    }
                }
            }
        } catch (e: IOException) {
            e.printStackTrace()
        }
        return outputStream.toByteArray()
    }

    /**
     * 生成
     *
     * @param content   类中的内容
     * @param classPath 类文件路径
     * @param className 类文件名称
     */
    @JvmStatic
    fun writeToFile(content: String, classPath: String, className: String) {
        var confirmationDialog:ConfirmationDialog<File> = ConfirmationDialog(object : ConfirmCallback<File> {
            override fun confirm(result: File?) {
                result?.let {
                    writer(it, content)
                }
            }
        })
        try {
            val folder = File(classPath)
            if (!folder.exists()) {
                val isCreateSuccess = folder.mkdirs()
            }
            val file = File("$classPath/$className")
            if (!file.exists()) {
                val isCreateSuccess = file.createNewFile()
                writer(file, content)
            } else {
                /**
                 * 是否覆盖原有文件
                 */
                confirmationDialog.apply {
                    param = file
                    setContent("<html>${file.path}<br>下已经存在文件<br>${file.name}<br>是否使用新文件覆盖该文件</html>")
                    pack()
                    isVisible = true
                }
            }
            LocalFileSystem.getInstance().refreshIoFiles(arrayListOf(file))
        } catch (e: IOException) {
            e.printStackTrace()
        }
    }

    private fun writer(file: File, content: String) {
        val fw = FileWriter(file.absoluteFile)
        val bw = BufferedWriter(fw)
        bw.write(content)
        bw.close()
    }

}